<?php
require_once 'Junction/Builder/Definition.php';
require_once 'Junction/Builder/Class.php';
require_once 'Junction/Utils/Xml/Exception.php';
require_once 'Junction/Utils/Xml/Handler.php';
require_once 'Junction/Utils/Xml/Parser.php';


class Junction_Builder_Handlers_Class implements Junction_Utils_Xml_Handler {
	
	/**
	 * @throws Junction_Utils_Xml_Exception
	 * 
	 * @return Junction_Builder_Class
	 */
	public function handle(SimpleXMLElement $root) {
	    $attrs = Junction_Utils_Xml_Parser::collectAttributes($root);
		$class = new Junction_Builder_Class($attrs[Junction_Builder_Definition::nameAttribute],
		                $attrs[Junction_Builder_Definition::tableAttribute]);
		
		$children = Junction_Utils_Xml_Parser::parse($root);
		
		$ids = $children->get(Junction_Builder_Definition::keyTag);
		$class->id = $ids[0];
		
		$class->properties = $children->get(Junction_Builder_Definition::propertyTag);
		
		$class->directReferences = $children->get(Junction_Builder_Definition::directReferenceTag);
		
		return $class;
	}
}
?>